Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed the theme changing when reload issue, Issue #1187 #1188

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

widejoy
Copy link

@widejoy widejoy commented Mar 4, 2025

Description

Description:

Ensures that the theme selection persists across page reloads using localStorage.
Fixes the theme toggle button to correctly display:
Improves logic by checking localStorage on component mount and applying the correct theme class to document.documentElement.

Related Issues

#1187

Changes Proposed

const [theme, setTheme] = useState("dark");

 useEffect(() => {
   const savedTheme =
     typeof window !== "undefined" ? localStorage.getItem("theme") : "dark";
   if (savedTheme) {
     setTheme(savedTheme);
     document.documentElement.classList.toggle("dark", savedTheme === "dark");
   }
 }, []);

 function toggleTheme() {
   const newTheme = theme === "dark" ? "light" : "dark";
   setTheme(newTheme);
   if (typeof window !== "undefined") {
     localStorage.setItem("theme", newTheme);
   }
   document.documentElement.classList.toggle("dark", newTheme === "dark");
 }

When the page reloads, the component runs the useEffect hook, which:

Retrieves the stored theme from localStorage (if available).
Applies the saved theme by updating the theme state and toggling the "dark" class on document.documentElement.
This ensures that the theme remains consistent after a reload.

The toggleTheme function:

Switches between "dark" and "light".
Saves the new theme in localStorage.
Updates the document class accordingly.
As a result, the theme selection persists across sessions.

Checklist

  • [*] I have read and followed the Contribution Guidelines.
  • [*] All new and existing tests passed.
  • [*] I have updated the documentation to reflect the changes I've made.
  • [*] My code follows the code style of this project.
  • [*] The title of my pull request is a short description of the requested changes.

Screenshots

Note to reviewers

Copy link

vercel bot commented Mar 4, 2025

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
dev-find ✅ Ready (Inspect) Visit Preview 💬 Add feedback Mar 4, 2025 5:46am

Copy link

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great job, @widejoy! 🎉 Thank you for opening a pull request. Your contribution is valuable and we appreciate your efforts to improve our project.

Soon the maintainers/owner will review it and provide you with feedback/suggestions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant